package top.ajun.common.utils;

/**
 * 响应 JSON 数据
 *
 * @author AJun
 * @date 2020/12/1
 */
public class R {

    private static final Integer OK_STATUS = CodeEnum.SUCCESS.getCode();
    private static final Integer ERROR_STATUS = CodeEnum.FAILED.getCode();

    private static final String DEFAULT_OK_MSG = CodeEnum.SUCCESS.getMsg();
    private static final String DEFAULT_ERROR_MSG = CodeEnum.FAILED.getMsg();

    /**
     * HTTP 状态码
     */
    private Integer status;

    /**
     * 是否成功
     */
    private Boolean success;

    /**
     * 附加响应消息
     */
    private String message;

    /**
     * 响应数据
     */
    private Object data;

    private R(Integer status, Boolean success, String message) {
        this.status = status;
        this.success = success;
        this.message = message;
    }

    private R(Integer status, Boolean success, Object data, String message) {
        this(status, success, message);
        this.data = data;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    /**
     * 成功响应
     */
    public static R ok(Integer status, Object data, String message) {
        return new R(status, true, data, message);
    }

    public static R ok(Object data, String message) {
        return R.ok(OK_STATUS, data, message);
    }

    public static R ok(Object data) {
        return R.ok(OK_STATUS, data, DEFAULT_OK_MSG);
    }

    public static R ok() {
        return R.ok(OK_STATUS, null, DEFAULT_OK_MSG);
    }

    /**
     * 失败响应
     */
    public static R error(Integer status, Object data, String message) {
        return new R(status, false, data, message);
    }

    public static R error(Object data, String message) {
        return R.error(ERROR_STATUS, data, message);
    }

    public static R error(Object data) {
        return R.error(ERROR_STATUS, data, DEFAULT_ERROR_MSG);
    }

    public static R error() {
        return R.error(ERROR_STATUS, null, DEFAULT_ERROR_MSG);
    }

    /**
     * toString
     */
    @Override
    public String toString() {
        return "R{" +
            "status=" + status +
            ", success=" + success +
            ", message='" + message + '\'' +
            ", data=" + data +
            '}';
    }

}
